home *** CD-ROM | disk | FTP | other *** search
- #pragma once on
- /*
- AppAEObj_pd.h
- © Bob Boylan 1996
-
- Revision History
- MacHack 1996 initial creation
- */
-
- // -----------------------------------------------------------------
- // Clone_ut ... a template class for reference counting
- //
- template <class Obj>
- class Clone_ut
- {
- public:
- // ctor
- Clone_ut( )
- {
- _Ref = nil;
- }
-
- Clone_ut( Obj *inOriginal )
- {
- _Ref = new SRef;
- if( _Ref == nil ) throw (long) memFullErr;
- _Ref->_RealObj = inOriginal;
- _Ref->_Count = 1;
- }
-
- // copy ctor
- Clone_ut( const Clone_ut &inOriginalClone )
- {
- _Ref = inOriginalClone._Ref;
- IncrementRefCount( _Ref );
- }
-
- // assignment
- Clone_ut & operator =( const Clone_ut &inOtherClone )
- {
- if( this != &inOtherClone )
- {
- SRefPtr theRef = inOtherClone._Ref;
- IncrementRefCount( theRef );
- DecrementRefCount();
- _Ref = theRef;
- }
- return *this;
- }
-
-
- // dtor
- ~Clone_ut()
- {
-
- DecrementRefCount();
- }
-
- // access to original ... sometimes you just need it
- Obj & operator *()
- {
- if( _Ref == nil )
- {
- Debugger();
- throw (long) memAdrErr;
- }
- return *(_Ref->_RealObj);
- }
-
- // for checking the validity
- Boolean Isnil() { return _Ref == nil; }
-
- protected:
- // the reference struct
- typedef struct {
- Obj *_RealObj; // the real obj
- Int_32 _Count; // and the number of references
- } SRef, *SRefPtr;
-
- // data
- SRefPtr _Ref;
-
-
- // DecrementRefCount - a worker
- void DecrementRefCount()
- {
- if( _Ref != nil )
- {
- --_Ref->_Count;
- if( _Ref->_Count == 0 )
- {
- delete _Ref->_RealObj;
- _Ref->_RealObj = nil;
- delete _Ref;
- _Ref = nil;
- }
- }
- }
-
- // IncrementRefCount - a worker
- void IncrementRefCount( SRefPtr &ioRef )
- {
- ++ioRef->_Count;
- }
-
-
- private:
- };
-